package com.ysm.smfu.fragment;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.google.gson.Gson;
import com.ysm.smfu.R;
import com.ysm.smfu.adapter.DefaultAdapter;
import com.ysm.smfu.bean.DetailInfo;
import com.ysm.smfu.bean.ShouKuanInfo;
import com.ysm.smfu.contans.SpKey;
import com.ysm.smfu.holder.BaseHolder;
import com.ysm.smfu.holder.MingXiHolder;
import com.ysm.smfu.protocal.BaseProtocal;
import com.ysm.smfu.protocal.MyStringCallBack;
import com.ysm.smfu.utils.JsonUtil;
import com.ysm.smfu.utils.PrintUtil;
import com.ysm.smfu.utils.ToastUtils;
import com.ysm.smfu.utils.UIUtils;
import com.ysm.smfu.view.RefreshLayout;
import com.ysm.smfu.welcome.SharedConfig;

import java.util.List;

/**
 * Created by Administrator on 2016/10/14.
 */
public class MingXiFragment extends BaseRefreshFragment<DetailInfo> {

    private String businessId;
    private ListView listView;
    private String name;
    private MyAdapter adapter;
    private RefreshLayout refresh;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = UIUtils.inflate(R.layout.mingxi);
        listView = (ListView) view.findViewById(R.id.mingxi_list);
        refresh = (RefreshLayout) view.findViewById(R.id.refresh);
        initData();
        getBusinessId();
//        获取详细订单数据
        getMingXiData();
        initRefresh(refresh, true, false);
        return view;
    }

    private void initData() {
        adapter = new MyAdapter(datas, listView);
        listView.setAdapter(adapter);
    }

    //获取businessId
    private void getBusinessId() {
        SharedPreferences sp = new SharedConfig(UIUtils.getContext()).getCommoneSp();
        name = sp.getString(SpKey.NAME, "");
        SharedPreferences currShared = new SharedConfig(UIUtils.getContext(), name).getPrivateSp();
        businessId = currShared.getString(SpKey.BUSSINESSID, "");
    }

    /**
     * 获取详细订单数据
     * /business/getPayList
     * businessId:商家ID
     */
    private void getMingXiData() {
        BaseProtocal.builder(true).getMingXiData(new MyStringCallBack() {
            @Override
            public void onSuccess(String response) {
                PrintUtil.printRespones("商户收款详情" , response);
                String state = JsonUtil.getState(response);
                if (TextUtils.equals(state, "1")) {
                    ShouKuanInfo shouKuanInfo = new Gson().fromJson(response, ShouKuanInfo.class);
                    List<DetailInfo> piList = shouKuanInfo.piList;
                    setLoadData(piList);

                    adapter.setDatas(datas);
                } else {
                    ToastUtils.showToast(JsonUtil.getMessage(response));
                    onRefreshError();
                }
            }

            @Override
            public void onFail(Exception e) {
                ToastUtils.showErrToast();
                onRefreshError();
            }
        }, businessId, page);
    }

    @Override
    public void load(boolean isLoad) {
        if (isLoad)
            page++;
        getMingXiData();
    }

    @Override
    public void refresh() {

    }


    class MyAdapter extends DefaultAdapter<DetailInfo> {

        public MyAdapter(List<DetailInfo> datas, ListView listView) {
            super(datas, listView, null);
        }

        @Override
        protected BaseHolder<DetailInfo> getHolder() {
            return new MingXiHolder();
        }
    }
}
